<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewAccount extends JView
{
	function display($tpl = null) {

		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		} 

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('account', 'view', 'account', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');	

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_ACCOUNT_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('account','edit','account','all'):1;

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displayForm() {

		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		$gid		= $user->get('gid');

		JHTML::_('behavior.mootools');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		
		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.bgiframe.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.ajaxQueue.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.min.js');
		//add css autocomplete
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.css');

		//ui
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-ui-1.9.1/themes/base/jquery.ui.all.css');
		//$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/jquery-1.8.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/external/jquery.bgiframe-2.1.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.core.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.widget.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.mouse.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.draggable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.position.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.resizable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.dialog.js');

		$document->addScriptDeclaration(
			'
				window.addEvent("domready", function() {
					$("bank_name").addEvent("change", function(event){
						var value = this.value;
						if(value == "other") {
							$("bankother").setProperty("style", "display:block");
						} else {
							$("bankother").setProperty("style", "display:none");
						}
					});
					
					$("account_type").addEvent("change", function(event){
						var value = this.value;
						if(value == "other") {
							$("typeother").setProperty("style", "display:block");
						} else {
							$("typeother").setProperty("style", "display:none");
						}
					});
				});
				
				function selectUser(cid, membername)
				{
					document.adminForm.membername.value=membername;
					document.adminForm.user_id.value=cid;
					checkexpire(cid);
					document.getElementById(\'sbox-window\').close();
				}
				
				jQuery(document).ready(function () {
					jQuery("#membername").autocomplete("index.php?option=com_ownservice&controller=account&task=viewmembers&format=raw&'.JUtility::getToken().'=1", {
					width: 260,
					max: 100,
					matchContains: false,
					mustMatch: false,
					selectFirst: false,
					dataType: "json",
					parse: function(data) {
						return jQuery.map(data, function(row) {
							return {
								data: row,
								value: row.name,
								result: row.name
							};
						});
					},
					formatItem: function(row) {
						return row.name;
					}
					}).result(function(e, row) {
						jQuery("#user_id").val(row.id);
						checkexpire(row.id);
					})

					jQuery("#account_no").on("keyup focus", function() {
						var account		= jQuery("#account_no").val();
						var num			= account.length;
						if(num < 10){
							jQuery("#save").attr("disabled", "disabled");
							jQuery("#shownum").css("color","red");
							jQuery("#shownum").css("font-weight", "bold");
						} else {
							jQuery("#save").removeAttr("disabled");
							jQuery("#shownum").css("color","black");
							jQuery("#shownum").css("font-weight", "normal");
						}
						jQuery("#shownum").text((num));
					});

				});

				function checkexpire(cid){
					jQuery.post("index.php?option=com_ownservice&controller=account&task=checkexpire&'.JUtility::getToken().'=1", { id:cid},
					function(data){
						if(data.days >0 && data.days <= 30){
							jQuery("#msg").text(\'ID expiring soon\');
							jQuery("#memberid").val(data.id);
							jQuery("#exdate").text(\'(\'+data.days+\')\'+\' \'+data.expiredate);
							jQuery("#dialog").dialog();
						} else if(data.days <= 0) {
							jQuery("#msg").text(\'ID Expired. Unable to make a transaction\');
							jQuery("#memberid").val(data.id);
							jQuery("#exdate").text(\'(\'+data.days+\')\'+\' \'+data.expiredate);
							jQuery("#save").attr(\'disabled\', \'true\'); 	
							jQuery("#dialog").dialog();	
						}
						
					}
					,\'json\'
					);
				}
			');	

		//Get data form the model
		$model		= $this->getModel();
		$row		= &$this->get('Data');
		$countries	= &$this->get('Countries');

		// check if it's an edit action
		if($row->id) {
				$canEdit	= ($gid<25)?Ownservice::checkAccess('account', 'edit', 'account', 'all'):1;
				if (!$canEdit) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		} else {
				$canAdd		= ($gid<25)?Ownservice::checkAccess('account', 'add', 'account', 'all'):1;
				if (!$canAdd) {
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		}

		//set page title
		if($row->id) {
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_ACCOUNT'));
		} else {	
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_ACCOUNT'));
		}
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		//build account type select list
		$lists['type'] = OwnserviceHelper::buildaccounttypeselect('account_type',$row->account_type, 1, 'class="inputbox required" style="width:250px;"');

		//build country select list
		$lists['country'] = OwnserviceHelper::buildcountryselect($countries, 'country',$row->country,1, 'class="inputbox required" style="width:250px;"');

		//build banks to select list
		$lists['bank'] = OwnserviceHelper::buildbanknamelist('bank_name', $row->bank_name, true, 'class="inputbox required" style="width:250px;"');

		JHTML::_('behavior.modal', 'a.modal');
		
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);
		//$this->assignRef('profile', $profile);

		parent::display($tpl);
	}
}
